package guda.house.web.action.sys.village.park;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.ParkingBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.form.village.ParkingEditForm;
import guda.house.biz.form.village.ParkingForm;
import guda.house.biz.query.BuildingQuery;
import guda.house.biz.query.ParkingQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dataobject.*;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by well on 15/11/15.
 */
@Controller
public class ParkingAction {

    @Autowired
    private ParkingBiz parkingBiz;

    @RequestMapping(value = "sys/village/parking/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        long villageId = RequestUtil.getInt(request, "villageId");
        if(villageId == 0){
            return "redirect:/error.htm?code="+ CommonResultCode.PARAM_MISS;
        }
        ParkingQuery parkingQuery = new ParkingQuery();
        parkingQuery.setVillageId(villageId);
        parkingQuery.setPageNo(pageId);
        parkingQuery.setPageSize(pageSize);
        modelMap.put("query",parkingQuery);
        BizResult bizResult = parkingBiz.list(parkingQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/village/parking/list.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }
    }

    @RequestMapping(value = "sys/village/parking/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, ParkingEditForm parkingEditForm,
                       BindingResult result, Map<String,Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = parkingBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            ParkingDO parkingDO = (ParkingDO)bizResult.data.get("parkingDO");
            parkingEditForm.initForm(parkingDO);
            bizResult = parkingBiz.queryForEdit(id);
            modelMap.putAll(bizResult.data);

            SuiteDO suiteDO =  (SuiteDO)bizResult.data.get("suiteDO");
            UnitDO unitDO = (UnitDO)bizResult.data.get("unitDO");
            BuildingDO buildingDO = (BuildingDO)bizResult.data.get("buildingDO");
            parkingEditForm.setVillageId(buildingDO.getVillageId());
            parkingEditForm.setBuildingId(buildingDO.getBuildingId());
            parkingEditForm.setUnitId(unitDO.getUnitId());
            parkingEditForm.setSuiteId(suiteDO.getSuiteId());
            OwnerDO ownerDO = (OwnerDO)bizResult.data.get("ownerDO");
            parkingEditForm.setOwnerId(ownerDO.getOwnerId());
            return "sys/village/parking/edit.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }



    @RequestMapping(value = "sys/village/parking/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, ParkingForm parkingForm,
                         BindingResult result) {
        long villageId = RequestUtil.getInt(request, "villageId");
        if(villageId == 0){
            return "redirect:/error.htm?code="+ CommonResultCode.PARAM_MISS;
        }
        parkingForm.setVillageId(villageId);
        BizResult bizResult = parkingBiz.queryForCreate(villageId);
        modelMap.putAll(bizResult.data);
        return "sys/village/parking/create.vm";
    }

    @RequestMapping(value = "sys/village/parking/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid ParkingForm parkingForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            BizResult bizResult = parkingBiz.queryForReset(parkingForm.getVillageId(), parkingForm.getBuyed());
            modelMap.putAll(bizResult.data);
            return "sys/village/parking/create.vm";
        }
        ParkingDO parkingDO = parkingForm.toDO();
        BizResult bizResult = parkingBiz.create(parkingDO);
        if (bizResult.success) {
            return "redirect:/sys/village/parking/list.htm?villageId="+parkingDO.getVillageId();
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/parking/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid ParkingEditForm parkingEditForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            BizResult bizResult = parkingBiz.queryForReset(parkingEditForm.getVillageId(), parkingEditForm.getBuyed());
            modelMap.putAll(bizResult.data);
            return "sys/village/building/edit.vm";
        }
        ParkingDO parkingDO = parkingEditForm.toDO();
        BizResult bizResult = parkingBiz.update(parkingDO);
        if (bizResult.success) {
            return "redirect:/sys/village/parking/list.htm?villageId="+parkingDO.getVillageId();
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/parking/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = parkingBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
